import { BasicMarker } from '../../js/BasicMarker.js';
import { BasicMarkers } from '../../js/BasicMarkers.js';
import { getMarkerIcon } from '../../js/icon.js';
import { netPointList, yardCoordinateHotelCount } from '@gis/lib/api';
import { useUserInfoStore } from '@gis/lib/pinia/userInfo.js';
import { handleCommonRes } from '@gis/lib/utils/resolver.js';
import { openHomestayListDialog } from '../../../../views/net-room/opt.js';
import { useStaticDataStore } from '@gis/lib/pinia/staticData.js';
import env from '@gis/lib/env.js';

function getRandomNumber(min, max) {
  return Math.floor(Math.random() * (max - min + 1) + min);
}

const VUE_APP_PROJECT_NAME = env.get('VUE_APP_PROJECT_NAME');

class Marker extends BasicMarker {
  constructor({ map, data }) {
    super({ map, data });
    /* this.type: 这个type主要用于筛选,需要和filter-legend里面的type相对应 */
    this.type = 'cywyf';
    super.create();
  }
  getIcon() {
    return getMarkerIcon(this.type);
  }
  getIconOption(icon) {
    return {
      content: `<div style="width: 52px;height: 98px;background: url(${icon}) center center no-repeat;background-size: 100% 100%"></div>`,
      offset: new AMap.Pixel(0, 0),
    };
  }
  getLabelContent() {
    return `<div class='marker_label'>
<p class='marker_label__name'>${this.data.yard_name}</p>
<p class='marker_label__count'>网约房套数：${this.data.hotel_total || 0}</p>
<p class='marker_label__count'>房间总数：${this.data.room_total || 0}</p>
<p class='marker_label__count'>今日入住：${this.data.checkIn ? this.data.checkIn : getRandomNumber(5, 15)}</p>
<p class='marker_label__count'>今日离店：${this.data.checkOut ? this.data.checkOut : getRandomNumber(5, 15)}</p>
</div>`;
  }

  onClick() {
    if (!VUE_APP_PROJECT_NAME) {
      openHomestayListDialog(this.data.yard_id);
    }
  }
}

export default class Cywyf extends BasicMarkers {
  constructor({ map }) {
    super();
    this.map = map;
  }
  createMarker(data) {
    return new Marker({ map: this.map, data });
  }
  async getDataAsync() {
    let data = [];
    // 静态页面使用假数据
    if (VUE_APP_PROJECT_NAME === 'git-net-house-static') {
      const { getMergedList } = useStaticDataStore();
      data = getMergedList([], 'wyfzdsj', { defaultValue: [], uniqueKey: 'yard_name' });
    } else {
      const { area_code } = useUserInfoStore();
      data = await handleCommonRes(netPointList, { area_code: area_code });
    }
    return data.map((item) => Object.assign(item, { lnglat: [item.longitude, item.latitude] }));
  }
}
